<?php
  require_once('./db.php');
  require_once('./header.php');

  // searching Start
  if(isset($_GET['search']) && !empty($_GET['search'])) {
      $search = $_GET['search'];
      $where = "WHERE `uname` LIKE '%$search%'";
  } else {
    $search = '';
    $where = '';
  }
  // search end


  $usersQuery = "SELECT * FROM `users` {$where}";
  $usersResult = mysql_query($usersQuery) or die(mysql_error());

  $fileName = date('Ymdhis').'.csv';
  $fp = fopen("./tmp/$fileName",'w+');
  $colNamesArr = array(
                    'ID',
                    'USER NAME',
                    'EMAIL',
                    'PASSWORD',
                    'ADDED DATE',
                    'MODIFIED DATE',
                    'STATUS',
                    'AVATAR',
                  );
  fputcsv($fp, $colNamesArr);

  while($user = mysql_fetch_assoc($usersResult)) {
    fputcsv($fp, $user);
  }
  fclose($fp);

  $fileserver_path = './tmp';
  $req_file = $fileName;

  header('Content-Description: File Transfer');
  header('Content-Type: application/force-download');
  header('Content-Length: ' . filesize("$fileserver_path/$req_file"));
  header('Content-Disposition: attachment; filename=' . $req_file);
  readfile("$fileserver_path/$req_file");
  exit;


?>

<?php
  require_once('./footer.php');
